package com.basketball.gym.controller;


import com.basketball.gym.annotation.ApiJsonObject;
import com.basketball.gym.annotation.ApiJsonProperty;
import com.basketball.gym.pojo.Finance;
import com.basketball.gym.pojo.ResponseData;
import com.basketball.gym.pojo.User;
import com.basketball.gym.pojo.Wallet;
import com.basketball.gym.service.WalletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 赵肖云
 * @since 2021-03-27
 */
@Api(tags = "余额管理")
@RestController
@RequestMapping("/gym/wallet")
public class WalletController {
    @Autowired
    private WalletService walletService;

    @ApiOperation("查询钱包")
    @GetMapping
    public ResponseData<Wallet> getWallet(@ApiIgnore @ModelAttribute("activeUser") User user) {
        return walletService.getWallet(user);
    }

    @ApiOperation("管理员充值")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PostMapping("/topUp")
    public ResponseData<Wallet> topUp(@ApiJsonObject(name = "AdminTopUp", value = {
            @ApiJsonProperty(key = "phone"),
            @ApiJsonProperty(key = "price")
    }) @RequestBody Map<String, Object> param) {
        String phone = (String) param.get("phone");
        Double price = (double) (int) param.get("price");
        return walletService.topUp(phone, price);
    }

    @ApiOperation("消耗入场券")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PostMapping("/ticket")
    public ResponseData<Wallet> useTicket(@ApiJsonObject(name = "UseTicket", value = {
            @ApiJsonProperty(key = "phone"),
            @ApiJsonProperty(key = "ticketNum")
    }) @RequestBody Map<String, Object> param) {
        String phone = (String) param.get("phone");
        Integer ticketNum = (Integer) param.get("ticketNum");
        return walletService.useTicket(phone, ticketNum);
    }

    @ApiOperation("消费余额")
    @PreAuthorize("hasAnyRole('admin','superAdmin')")
    @PostMapping("/balance")
    public ResponseData<Object> useBalance(@Validated(Finance.adminUse.class) @RequestBody Finance finance){
        return walletService.useBalance(finance);
    }

    @ApiOperation("根据手机号查询钱包")
    @GetMapping("/phone")
    public ResponseData<Wallet> getWalletByPhone(@RequestParam String phone){
        return walletService.getWalletByPhone(phone);
    }

    @ApiOperation("管理员查询钱包")
    @GetMapping("/admin")
    public ResponseData<List<Wallet>> adminGetWallet(String phone){
        return walletService.adminGetWallet(phone);
    }

    @ApiOperation("swagger测试接口")
    @PostMapping("/zzzzzzzzzzzzzzzzzz")
    public void zzzzzzzzzzzzzzzzzzzzzzz(@ApiJsonObject(name = "zzzzzzzzzzzzzzz", value = {
            @ApiJsonProperty(key = "encryptedData", description = "加密字符串"),
            @ApiJsonProperty(key = "iv", description = "从微信获取的iv")
    }) @RequestBody Map<String, Object> param) {

    }
}

